﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
//using System.ServiceModel.Syndication;
using System.Reflection;
using System.Runtime.Remoting.Channels;
using System.Web;
using System.Web.UI.WebControls;
using gnet1.ExtensionMethods;


namespace gnet1.Models
{
  public class DaveViewModel
  {
    public List<string> Data { get; set; }
    public int Count { get; set; }
    public string PictureUrl { get; set; }
    //public SyndicationFeed Feed { get; set; }
    public List<string> PictureList { get; set; }
    public List<string> VideoList { get; set; }
    //string connStr1 = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    string connStr = "Data Source=iegyderpct.database.windows.net;Initial Catalog=Gnazzo;Integrated Security=False;User ID=jgnazzo;Password=Ozzang99!;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False";
    private string queryString = "SELECT LastName from Test1";
    public string Url { get; set; }
    public string Folder { get; set; }

    /// <summary>
    /// Background image for Home Page
    /// </summary>
    /// <returns></returns>
    public string GetPictureUrl()
    {
      var random = new Random();
      int folder = random.Next(1, 7);

      int num;

      switch (folder)
      {
        case 1:
          num = random.Next(1, 998);
          return string.Format("http://www.gnazzo.net/imagesSave/Nikon{0}/DSC_{1}.jpg", folder, num.ToString("0000"));

        case 2:
          num = random.Next(1, 981);
          return string.Format("http://www.gnazzo.net/imagesSave/Nikon{0}/DSC_{1}.jpg", folder, num.ToString("0000"));

        case 3:
          num = random.Next(1, 1000);
          return string.Format("http://www.gnazzo.net/imagesSave/Nikon{0}/DSC_{1}.jpg", folder, num.ToString("0000"));

        case 4:
          num = random.Next(1, 687);
          return string.Format("http://www.gnazzo.net/imagesSave/Nikon{0}/DSC_{1}.jpg", folder, num.ToString("0000"));

        case 5:
          num = random.Next(1, 453);
          return string.Format("http://www.gnazzo.net/imagesSave/Nikon{0}/DSC_{1}.jpg", folder, num.ToString("0000"));

        case 6:
          num = random.Next(1, 411);
          return string.Format("http://www.gnazzo.net/imagesSave/Nikon{0}/DSC_{1}.jpg", folder, num.ToString("0000"));

        case 7:
          num = random.Next(1376, 8031);
          return string.Format("http://www.gnazzo.net/imagesSave/100_{0}.jpg", num.ToString("0000"));
      }

      return String.Empty;
    }

    /// <summary>
    /// Get list of pictures from another website - through FTP
    /// </summary>
    /// <param name="url"></param>
    /// <param name="folder"></param>
    /// <returns></returns>
    public List<string> GetPictureList(string url, string folder)
    {
      string line;
      string picture;
      string type;

      // Get the object used to communicate with the server.
      FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + url + "/" + folder);
      request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

      // This example assumes the FTP site uses anonymous logon.
      request.Credentials = new NetworkCredential("jgnazzo", "Ozzang99!");

      FtpWebResponse response = (FtpWebResponse)request.GetResponse();

      Stream responseStream = response.GetResponseStream();
      StreamReader reader = new StreamReader(responseStream);

      var list = new List<string>();
      while (!reader.EndOfStream)//Read file name   
      {
        line = reader.ReadLine();

        if (line != null)
        {
          try
          {
            picture = line.Substring(39);
            type = picture.Substring(picture.Length - 4, 4).ToLower();
            if (type.IsIn(".jpg", ".bmp"))
              list.Add(picture);
          }
          catch (Exception ex)
          {

          }
        }
      }

      reader.Close();
      response.Close();

      return list;
    }

    /// <summary>
    /// Get list of pictures from another website - through Directory Lookup
    /// </summary>
    /// <param name="folder"></param>
    /// <returns></returns>
    public List<string> GetPictureList(string folder)
    {
      var realFolder = AppDomain.CurrentDomain.BaseDirectory + folder;
      var result = Directory.GetFiles(realFolder, "*.*", SearchOption.TopDirectoryOnly).Where(s => s.ToLower().EndsWith(".jpg") || s.ToLower().EndsWith(".bmp"));
      return result.Select(Path.GetFileName).ToList();
    }

    public List<string> GetVideoList(string url, string folder)
    {
      string line;
      string movie;
      string type;

      // Get the object used to communicate with the server.
      FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + url + "/" + folder);
      request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

      // This example assumes the FTP site uses anonymous logon.
      request.Credentials = new NetworkCredential("jgnazzo", "Ozzang99!");

      FtpWebResponse response = (FtpWebResponse)request.GetResponse();

      Stream responseStream = response.GetResponseStream();
      StreamReader reader = new StreamReader(responseStream);

      var list = new List<string>();
      while (!reader.EndOfStream)//Read file name   
      {
        line = reader.ReadLine();

        if (line != null)
        {
          try
          {
            movie = line.Substring(39);
            type = movie.Substring(movie.Length - 4, 4).ToLower();
            if (type.IsIn(".mov", ".mp3"))
              list.Add(movie);
          }
          catch (Exception ex)
          {

          }
        }
      }

      reader.Close();
      response.Close();

      return list;
    }

    public List<string> GetVideoList(string folder)
    {
      var realFolder = AppDomain.CurrentDomain.BaseDirectory + folder;
      var result = Directory.GetFiles(realFolder, "*.mov", SearchOption.TopDirectoryOnly);
      return result.Select(Path.GetFileName).ToList();
    }

    public List<string> GetData()
    {
      var list = new List<string>();
      using (SqlConnection connection = new SqlConnection(connStr))
      {
        SqlCommand command = new SqlCommand(queryString, connection);
        try
        {
          connection.Open();
          SqlDataReader reader = command.ExecuteReader();
          while (reader.Read())
          {
            list.Add(reader[0].ToString());
          }
          return list;
        }
        catch (Exception)
        {
          return null;
        }
      }
    }
  }
}